package com.zylix.simon.drawings.notes;

import java.io.File;

import com.example.android.notepad.NoteEditor;
import com.example.android.notepad.R;
import com.example.android.notepad.NotePad.NoteColumns;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class DrawViewActivity extends Activity {
	private static final String TAG = "DrawViewActivity";

    private static final String[] PROJECTION = new String[] {
        NoteColumns._ID, // 0
        NoteColumns.NOTE, // 1
        NoteColumns.TITLE, // 2
    };
	
    private static final int STATE_EDIT = 0;
    private int mState;
    private Uri mUri;
    private Cursor mCursor;
    private String savedNoteFileName;

    private static File APP_FILE_PATH = new File("/sdcard/DrawingNotes");
    /** The index of the note column */
    private static final int COLUMN_INDEX_NOTE = 1;
	int i;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bitmap mBitmap;
		final Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		
        // Do some setup based on the action being performed.
        final String action = intent.getAction();
        
		if (Intent.ACTION_EDIT.equals(action)) {
            // Requested to edit: set that state, and the data being edited.
            mUri = intent.getData();
        } 
        // The new entry was created, so assume all will end well and
        // set the result to be returned.
        setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
		
		savedNoteFileName = bundle.getString("filename");
		Log.i(TAG, savedNoteFileName);
        mBitmap = BitmapFactory.decodeFile(APP_FILE_PATH + savedNoteFileName);
        BitmapView bv = new BitmapView(this, mBitmap);
        this.setContentView(bv);
        mCursor = managedQuery(mUri, PROJECTION, null, null, null);
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu from XML resource
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.editor_options_menu, menu);

        // Append to the
        // menu items for any other activities that can do stuff with it
        // as well.  This does a query on the system for any activities that
        // implement the ALTERNATIVE_ACTION for our data, adding a menu item
        // for each one that is found.
        Intent intent = new Intent(null, getIntent().getData());
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
                new ComponentName(this, DrawViewActivity.class), null, intent, 0, null);

        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (mState == STATE_EDIT) {
            menu.setGroupVisible(R.id.menu_group_edit, true);
            menu.setGroupVisible(R.id.menu_group_insert, false);
            
            menu.findItem(R.id.menu_save).setEnabled(false);
            menu.findItem(R.id.menu_revert).setEnabled(false);

        } else {
            menu.setGroupVisible(R.id.menu_group_edit, false);
            menu.setGroupVisible(R.id.menu_group_insert, true);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case R.id.menu_save:

            break;
        case R.id.menu_delete:
            deleteNote();
            break;
        case R.id.menu_revert:
        case R.id.menu_discard:

            break;
        }
        return super.onOptionsItemSelected(item);        
    }
    
    /**
     * Take care of deleting a note.  Simply deletes the entry.
     */
    private final void deleteNote() {
    	boolean isSuccess;
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
            getContentResolver().delete(mUri, null, null);
            File f = new File(savedNoteFileName);
            isSuccess = f.delete();
            if(!isSuccess)
            	Toast.makeText(DrawViewActivity.this,"Draw note deleted!", Toast.LENGTH_LONG).show();
        }
        finish();
    }
    
}
